import HRequest from './request'
import { useUserStore } from "@/store/user";

const API_BASE_URL = import.meta.env.VITE_BASE_URL
const TIME_OUT = 30000

const hRequest = new HRequest({
  baseURL: API_BASE_URL,
  timeout: TIME_OUT,
  headers: {
    'Content-Type': 'application/json'
  },
  withCredentials: false, // 跨域请求是否需要携带 cookie
  interceptorHooks: {
    requestInterceptor: (config) => {
      const userStore = useUserStore()
      const token = userStore.getToken
      if (token) {
        config.headers.Authorization = `${token}`
      }

      if (userStore.getSelectedDept._id) {
        config.headers['Dept-Id'] = userStore.getSelectedDept._id
      }
      return config
    },
    requestInterceptorCatch: (err) => {
      return err
    },
    responseInterceptor: (res) => {
      return res.data
    },
    responseInterceptorCatch: (err) => {
      return err
    }
  }
})

export { hRequest }

export const baseUrl = API_BASE_URL
